package com.lanyun.controller;


import cn.hutool.json.JSONUtil;
import cn.org.atool.fluent.mybatis.base.crud.IQuery;
import cn.org.atool.fluent.mybatis.metadata.JoinType;
import com.lanyun.common.R;
import com.lanyun.entity.ImagelistEntity;
import com.lanyun.entity.ScenicEntity;
import com.lanyun.entity.vo.ImageReturn;
import com.lanyun.mapper.ImagelistMapper;
import com.lanyun.mapper.ScenicMapper;
import com.lanyun.wrapper.ImagelistQuery;
import com.lanyun.wrapper.ScenicQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @author: LanYun
 * @date: 2022/3/2 14:43
 * @Version: 1.0
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/images")
public class ImageController {

    @Autowired
    @Qualifier("imagelistMapper")
    ImagelistMapper imagelistMapper;
    @Autowired
    @Qualifier("scenicMapper")
    private ScenicMapper scenicMapper;

    /**
     * @Description 增加一张图片或者修改一张图片 
     * @Param entity: 
     * @Author LanYun
     * @Date 2022/3/5 15:49
     */
    @PostMapping("/addOne")
    public R saveOne(@RequestBody ImagelistEntity entity){
        System.out.println(entity);
        ScenicEntity one = scenicMapper.findOne(new ScenicQuery().where().id().eq(entity.getImageId()).end());
        if(one!=null){
            imagelistMapper.saveOrUpdate(entity);
            return R.ok();
        }else{
            return R.error();
        }
    }

    /**
     * @Description 删除一张图片 
     * @Param id: 
     * @Author LanYun
     * @Date 2022/3/5 15:49
     */
    @DeleteMapping("/deleteOne/{id}")
    public R removeOne(@PathVariable("id") Long id){
        imagelistMapper.deleteById(id);
        return R.ok();
    }
    /**
     * @Description 根据id查找图片列表
     * @Param id: 前端传的图片id
     * @Author LanYun
     * @Date 2022/3/5 15:50
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") Long id) {
        //涉及两张表的联查
        //1 构建景点查询对象
        ScenicQuery leftQuery = new ScenicQuery("sc");
        //2 构建图片查询对象
        ImagelistQuery rightQuery = new ImagelistQuery("il");
        // 查询列
        rightQuery.select("id", "image_url");
        leftQuery.select("name").where.id().eq(id);
        // 联查对象，景点的ID=图片表的ImageID
        IQuery joinQuery = leftQuery.join(JoinType.LeftJoin, rightQuery).on(l -> l.where.id(), r -> r.where.imageId()).endJoin().build();
        // 查询数据库的结果
        List<Map<String, Object>> maps = scenicMapper.listMaps(joinQuery);
        // 封装返回给前端的结果
        List<ImageReturn> imageReturns = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            imageReturns.add(JSONUtil.toBean(JSONUtil.toJsonStr(map), ImageReturn.class));
        }
        return R.ok().data("list", imageReturns);
    }

//    =====================================小程序前端的接口==========================================

    @GetMapping("/img/{id}")
    public R selectIUrls(@PathVariable("id") Long id){
        ImagelistQuery imagelistQuery = new ImagelistQuery();
        imagelistQuery.select("image_url").where().imageId().eq(id);
        List<String> urls = scenicMapper.listObjs(imagelistQuery);
        return R.ok().data("urls",urls);
    }





}
